import axios from 'axios'
import { API_BASE_URL } from '@/config'

const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 15000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// Request interceptor
api.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// Response interceptor
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    const message = error.response?.data?.message || error.message || 'Request failed'
    console.error('API Error:', message)
    return Promise.reject(error)
  }
)

// API Methods
export const categoryAPI = {
  getAll: () => api.get('/categories'),
  getById: (id) => api.get(`/categories/${id}`)
}

export const productAPI = {
  getAll: (params) => api.get('/products', { params }),
  getById: (id) => api.get(`/products/${id}`),
  getHot: () => api.get('/products', { params: { is_hot: 1, limit: 6 } })
}

export const storeAPI = {
  getInfo: () => api.get('/store')
}

export const contactAPI = {
  submit: (data) => api.post('/contact', data)
}

export default api

